home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / AbstractAction.java next >
Encoding:
Java Source  |  1999-05-28  |  5.9 KB  |  189 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)AbstractAction.java    1.25 98/08/28
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package javax.swing;
  15.  
  16. import java.awt.*;
  17. import java.awt.event.*;
  18. import java.beans.*;
  19. import java.util.Hashtable;
  20. import java.io.Serializable;
  21. import javax.swing.event.SwingPropertyChangeSupport;
  22.  
  23. /**
  24.  * This class provides default implementations for the JFC Action 
  25.  * interface. Standard behaviors like the get and set methods for
  26.  * Action object properties (icon, text, and enabled) are defined
  27.  * here. The developer need only subclass this abstract class and
  28.  * define the <code>actionPerformed</code> method. 
  29.  * <p>
  30.  * <strong>Warning:</strong>
  31.  * Serialized objects of this class will not be compatible with 
  32.  * future Swing releases.  The current serialization support is appropriate
  33.  * for short term storage or RMI between applications running the same
  34.  * version of Swing.  A future release of Swing will provide support for
  35.  * long term persistence.
  36.  *
  37.  * @version 1.25 08/28/98
  38.  * @author Georges Saab
  39.  * @see Action
  40.  */
  41. public abstract class AbstractAction implements Action, Cloneable, Serializable 
  42. {
  43.     protected boolean enabled = true;
  44.     // Will be replaced by a lighter weight storage mechanism soon!
  45.     private Hashtable keyTable = new Hashtable(5);
  46.  
  47.     /**
  48.      * Defines an Action object with a default description string
  49.      * and default icon.
  50.      */
  51.     public AbstractAction() {}
  52.     
  53.     /**
  54.      * Defines an Action object with the specified description string
  55.      * and a default icon.
  56.      */
  57.     public AbstractAction(String name) {
  58.     putValue(Action.NAME, name);
  59.     }
  60.  
  61.     /**
  62.      * Defines an Action object with the specified description string
  63.      * and a the specified icon.
  64.      */
  65.     public AbstractAction(String name, Icon icon) {
  66.     this(name);
  67.     putValue(Action.SMALL_ICON, icon);
  68.     }
  69.     
  70.     /** 
  71.      * Gets the Object associated with the specified key.
  72.      *
  73.      * @return the Object stored with this key
  74.      * @see Action#getValue
  75.      */
  76.     public Object getValue(String key) {
  77.     return keyTable.get(key);
  78.     }
  79.     
  80.     /** 
  81.      * Sets the Value associated with the specified key.
  82.      *
  83.      * @param key  the String that identifies the stored object
  84.      * @param newValue the Object to store using this key
  85.      * @see Action#putValue 
  86.      */
  87.     public synchronized void putValue(String key, Object newValue) {
  88.     Object oldValue = null;
  89.     if (keyTable.containsKey(key))
  90.         oldValue = keyTable.get(key);
  91.     if (newValue != null)
  92.         keyTable.put(key,newValue);
  93.     firePropertyChange(key, oldValue, newValue);
  94.     }
  95.  
  96.     /**
  97.      * Returns true if the action is enabled.
  98.      *
  99.      * @return true if the action is enabled
  100.      * @see Action#isEnabled
  101.      */
  102.     public boolean isEnabled() {
  103.     return enabled;
  104.     }
  105.  
  106.     /**
  107.      * Enables or disables the action.
  108.      *
  109.      * @param newValue  true to enable the action, false to
  110.      *                  disable it
  111.      * @see Action#setEnabled
  112.      */
  113.     public synchronized void setEnabled(boolean newValue) {
  114.     boolean oldValue = this.enabled;
  115.     this.enabled = newValue;
  116.     firePropertyChange("enabled", 
  117.                new Boolean(oldValue), new Boolean(newValue));
  118.     }
  119.  
  120.     /*
  121.      * If any PropertyChangeListeners have been registered, the
  122.      * changeSupport field describes them.
  123.      */
  124.     protected SwingPropertyChangeSupport changeSupport;
  125.  
  126.     /**
  127.      * Support for reporting bound property changes.  This method can be called
  128.      * when a bound property has changed and it will send the appropriate
  129.      * PropertyChangeEvent to any registered PropertyChangeListeners.
  130.      */
  131.     protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
  132.         if (changeSupport == null) {
  133.             return;
  134.         }
  135.         changeSupport.firePropertyChange(propertyName, oldValue, newValue);
  136.     }
  137.  
  138.  
  139.     /**
  140.      * Add a PropertyChangeListener to the listener list.
  141.      * The listener is registered for all properties.
  142.      * <p>
  143.      * A PropertyChangeEvent will get fired in response to setting
  144.      * a bound property, e.g. setFont, setBackground, or setForeground.
  145.      * Note that if the current component is inheriting its foreground, 
  146.      * background, or font from its container, then no event will be 
  147.      * fired in response to a change in the inherited property.
  148.      *
  149.      * @param listener  The PropertyChangeListener to be added
  150.      *
  151.      * @see Action#addPropertyChangeListener 
  152.      */
  153.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
  154.         if (changeSupport == null) {
  155.         changeSupport = new SwingPropertyChangeSupport(this);
  156.         }
  157.         changeSupport.addPropertyChangeListener(listener);
  158.     }
  159.  
  160.  
  161.     /**
  162.      * Remove a PropertyChangeListener from the listener list.
  163.      * This removes a PropertyChangeListener that was registered
  164.      * for all properties.
  165.      *
  166.      * @param listener  The PropertyChangeListener to be removed
  167.      *
  168.      * @see Action#removePropertyChangeListener 
  169.      */
  170.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
  171.         if (changeSupport == null) {
  172.             return;
  173.         }
  174.         changeSupport.removePropertyChangeListener(listener);
  175.     }
  176.  
  177.     /**
  178.      * Clone the abstract action. This gives the clone
  179.      * its own copy of the key/value list,
  180.      * which is not handled for you by Object.clone()
  181.      **/
  182.  
  183.     protected Object clone() throws CloneNotSupportedException {
  184.     AbstractAction newAction = (AbstractAction)super.clone();
  185.     newAction.keyTable = (Hashtable)keyTable.clone();
  186.     return newAction;
  187.     }
  188. }
  189.